package com.darwin.darwinUI.controls
{
	import com.darwin.darwinUI.core.UIComponent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	/**
	 * 滚动条组件
	 * @author lipi
	 */
	public class ScrollBar extends UIComponent
	{
		public function ScrollBar()
		{
			super();
			backgroundAuto = false;
			_bar = new UIComponent();
			_bar.backgroundAuto = false;
			setBarSize();
			addChild(_bar);
			_bar.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			this.addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
			this.addEventListener(Event.REMOVED_FROM_STAGE,removeFromStageHandler);
		}
		
		public static const VERTICAL:String = "vertical";
		public static const HORIZONTAL:String = "horizontal";
		
		//滑动条
		private var _bar:UIComponent;
		
		
		
		private var _downX:Number = 0;
		private var _downY:Number = 0;
		private var _downCurrentValue:Number = 0;
		
		//鼠标按下处理
		private function mouseDownHandler(e:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			_downX = e.stageX;
			_downY = e.stageY;
			_downCurrentValue = this.targetObject[this.targetCurrentValue];
		}
		
		//鼠标放开事件处理，移除原有的侦听器
		private function mouseUpHandler(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
//			_bar.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			
		}
		
		//鼠标移动处理
		private function mouseMoveHandler(e:MouseEvent):void
		{
			if(this.direction == ScrollBar.VERTICAL)
			{
				var $moveValue:Number = e.stageY - _downY;
				
				var $unit:Number = targetObject[targetMaxValue] / this.height;
				
				var $newValue:Number = _downCurrentValue+$moveValue * $unit;
				if($newValue < 0 ) $newValue = 0;
				else if($newValue > targetObject[targetMaxValue] - targetObject[targetPageValue])
				{
					$newValue = targetObject[targetMaxValue] - targetObject[targetPageValue];
				}
				
				targetObject[targetCurrentValue] = $newValue;
				
			}
		}
		
		
		//添加到舞台处理
		private function addedToStageHandler(e:Event):void
		{
			addEventListener(Event.ENTER_FRAME,enterFrameHandler);
		}
		
		//从舞台移除处理
		private function removeFromStageHandler(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
		}
		
		//帧处理事件
		private function enterFrameHandler(e:Event):void
		{
			setBarSize();
		}
		
		
		private var _targetObject:Object;
		/**
		 * 滚动条绑定的对象
		 */
		public function get targetObject():Object
		{
			return _targetObject;
		}

		public function set targetObject(value:Object):void
		{
			_targetObject = value;
			setBarSize();
		}
		
		
		private var _targetMaxValue:String;

		/**
		 * 目标最大值，即内容的值
		 */
		public function get targetMaxValue():String
		{
			return _targetMaxValue;
		}

		public function set targetMaxValue(value:String):void
		{
			_targetMaxValue = value;
			setBarSize();
		}
		
		/**
		 * 目标的一页大小
		 */
		private var _targetPageValue:String;

		public function get targetPageValue():String
		{
			return _targetPageValue;
		}

		public function set targetPageValue(value:String):void
		{
			_targetPageValue = value;
			setBarSize();
		}
		
		private var _targetCurrentValue:String;
		/**
		 * 目标当前值
		 */
		public function get targetCurrentValue():String
		{
			return _targetCurrentValue;
		}

		public function set targetCurrentValue(value:String):void
		{
			_targetCurrentValue = value;
			setBarSize();
		}
		
		
		
		private var _direction:String = "vertical";
		
		/**
		 * 标记是垂直滚动还是水平滚动
		 * @param value
		 */
		public function set direction(value:String):void
		{
			_direction = value;
			setBarSize();
		}
		
		public function get direction():String
		{
			return _direction;
		}
		
		private var _barBackground:DisplayObject;
		/**
		 * 滑动条背景
		 */
		public function get barBackground():DisplayObject
		{
			return _barBackground;
		}

		public function set barBackground(value:DisplayObject):void
		{
			_barBackground = value;
			_bar.background = value;
		}
		
		
		override public function set width(value:Number):void
		{
			super.width = value;
			setBarSize();
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			setBarSize();
		}
		
		private var _preMaxValue:Number = 0;
		private var _prePageValue:Number = 0;
		private var _preCurrentValue:Number = 0;
		private var _preHeight:Number = 0;
		
		/**
		 * 设置滑动条宽度或高度
		 */
		private function setBarSize():void
		{
			var t:Number = getTimer();
			if(direction == ScrollBar.VERTICAL)
			{
				if(targetObject != null && targetMaxValue != null && targetPageValue != null && targetCurrentValue != null)
				{
					var $maxValue:Number = targetObject[targetMaxValue];
					var $pageValue:Number = targetObject[targetPageValue];
					var $currentValue:Number = targetObject[targetCurrentValue];
					var $h:Number = this.height;
					
					if(_preMaxValue != $maxValue || _prePageValue != $pageValue|| 
						_preCurrentValue != $currentValue || _preHeight != $h)
					{
						var $barHeight:Number = $pageValue/$maxValue*$h;
						var $barY:Number = $h/$maxValue*$currentValue;
						if($barHeight > $h)
						{
							$barHeight = $h;
						}
						
						if(_bar.width != this.width) _bar.width = this.width;
						if(_bar.height != $barHeight) _bar.height = $barHeight;
						
						_bar.y = $barY;
						
						_preMaxValue = $maxValue;
						_prePageValue = $pageValue;
						_preCurrentValue = $currentValue;
						_preHeight = $h;
					}
				}
			}
			else
			{
				_bar.height = this.height;
			}
		}
		
		
	}
}